<template>

	<section>
		<!--工具条--><!--  高级查询相关-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="部门名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
    <el-table :data="types" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
<!--      批量删除按钮-->
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="数据字典名" width="120" >
        <!--        自定义状态展示列表-->
        <template scope="scope">
          <span @dblclick="popDrawer(scope.$index, scope.row)" v-html="scope.row.name"></span>
        </template>
      </el-table-column>
      <el-table-column prop="sn" label="部门编号" width="100" >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
<!--      批量删除的按钮-->
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="total"
                     style="float:right;">
			</el-pagination>
		</el-col>
<!--    id-->
<!--    sn-->
<!--    name-->
<!--    dirPath-->
<!--    state-->
<!--    manager_id-->
<!--    parent_id-->

		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="字典类型" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="部门编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
    <!--抽屉-->
    <el-drawer
        title="数据字典明细"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <!--列表-->
      <el-table border :data="gridData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="name" label="品牌名称" width="100" >
        </el-table-column>
        <el-table-column prop="types_id" label="品牌类型" width="160" >
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot="header" slot-scope="scope">
            <el-button type="success" size="small" @click="handleAdd02(scope.$index, scope.row)">
              新增
            </el-button>
          </template>
          <template scope="scope">
            <el-button size="small" @click="handleEdit02(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDel02(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--抽屉的工具条-->
      <el-col :span="24" class="toolbar">
        <!--      批量删除的按钮-->
        <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
        <el-pagination layout="prev, pager, next"
                       @current-change="handleCurrentChange02"
                       :page-size="pageSize02"
                       :total="total02"
                       style="float:right;">
        </el-pagination>
      </el-col>

    </el-drawer>
    <!--抽屉的编辑界面-->
    <el-dialog :title="title02" :visible.sync="editFormVisible02" :close-on-click-modal="false">
      <el-form :model="editForm02" label-width="80px"  ref="editForm02">
        <el-form-item label="品牌名称" prop="name">
          <el-input v-model="editForm02.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌类型" prop="sn">
          <el-input v-model="editForm02.types_id" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit02" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					keyword: ''
				},
        title:"",
        title02:"",
        row:"",
				types: [],
				total: 0,
				currentPage: 1,
        pageSize:2,
        total02: 0,
        currentPage02: 1,
        pageSize02:2,
				listLoading: false,
				sels: [],//列表选中列
        editFormVisible02:false,
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
          sn: [
            { required: true, message: '请输入部门编号', trigger: 'blur' }
          ]
				},
				//编辑界面数据
				editForm: {
          id:'',
          sn:null,
          name:"",
          state:1,
          parent:null,
          department:null
				},
        editForm02: {
          id:'',
          name:"",
          types_id:""
        },
        table: false,
        dialog: false,
        loading: false,
        gridData:[],
        detail:{

        }
			}
		},
		methods: {
      getEmployees (){
        this.$http.get("/emp").then(result=>{
          console.debug(result)
          this.employees=result.Object
          console.debug(result.Object)
        })
      },
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getTypes();
			},
      handleCurrentChange02(val) {
        this.currentPage02 = val;
        this.popDrawer02()
      },
      search(){

        let para = {
          currentPage:1,
          pageSize:this.pageSize,
          keyword: this.filters.keyword
        };
        //开启盲等
        this.listLoading = true;
        this.$http.post("/type",para).then(res=>{
          //  数据请求成功之后关闭忙等
          this.listLoading=false;
          //  获取后端返回的数据
          console.debug(res)
          this.types=res.data.rows;
          this.total=res.data.total
        })
      },

			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
					this.$http.delete("/type/"+row.id).then(result=>{
					//  关闭盲灯
            this.listLoading = false;
            // 1.获取后端传递的AjaxResult对象
            result = result.data;
            // 2.判断删除是否成功
            if (result.success){
              // 删除成功
              // 1.给出成功提示
              this.$message({
                message: result.msg,
                type: 'success'
              });
              // 2.修改页码为第一页
              this.currentPage = 1;
              // 3.调用函数刷新列表
              this.getTypes();
            }else {
              // 删除失败
              // 删除成功
              // 1.给出失败提示
              this.$message({
                message: result.msg,
                type: 'error'
              });
              // 2.修改页码为第一页
              this.currentPage = 1;
              // 3.调用函数刷新列表
              this.getTypes();
            }
          })
				}).catch(result=>{
				//  关闭盲灯
          this.listLoading=false;
        //  给出失败提示
          this.$message({
            message: '系统繁忙，请重试！',
            type: 'error'
          });
        }).catch(()=>{

        });
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.title="编辑";
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);

			},
      //显示编辑界面
      handleEdit02: function (index, row) {
        this.title02="编辑";
        this.editFormVisible02 = true;
        this.editForm02 = Object.assign({}, row);

      },
			//显示新增界面
			handleAdd: function () {
        this.title="新增";
				this.editFormVisible = true;
				this.editForm = {
          id:'',
          sn:null,
          name:"",
          state:1,
          employee:null,
          parent:null
				};
			},
      //显示新增界面
      handleAdd02: function (index,row) {
        this.title02="新增";
        this.editFormVisible02 = true;
        this.editForm02 = {
          id:'',
          name:"",
          types_id:""
        };
      },
			//编辑提交按钮
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);//表单数据拷贝

            //  发送axios请求
              this.$http.put("/type",para).then(result=>{
            //  关闭盲灯
                this.editLoading = false;
                // 1.获取后端传递的AjaxResult对象
                result = result.data;
                // 2.判断提交是否成功
                if (result.success){
                  // 提交成功
                  // 1.给出成功提示
                  this.$message({
                    message: result.msg,
                    type: 'success'
                  });
                  // 2.修改页码为第一页
                  this.currentPage = 1;
                  // 3.调用函数刷新列表
                  this.getTypes();
                }else {
                  // 1.给出失败提示
                  this.$message({
                    message: result.msg,
                    type: 'error'
                  });
                  // 2.修改页码为第一页
                  this.currentPage = 1;
                  // 3.调用函数刷新列表
                  this.getTypes();
                }
              })
            }).catch(result=>{
              //  关闭盲灯
              this.editLoading=false;
              //  给出失败提示
              this.$message({
                message: '系统繁忙，请重试！',
                type: 'error'
              });
              })
            this.editFormVisible=false
					}
				});
			},
      //编辑提交按钮
      editSubmit02: function () {
        this.$refs.editForm02.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true;
              //NProgress.start();
              let para = Object.assign({}, this.editForm02);//表单数据拷贝

              //  发送axios请求
              this.$http.put("/detail",para).then(result=>{
                //  关闭盲灯
                this.editLoading = false;
                // 1.获取后端传递的AjaxResult对象
                result = result.data;
                // 2.判断提交是否成功
                if (result.success){
                  // 提交成功
                  // 1.给出成功提示
                  this.$message({
                    message: result.msg,
                    type: 'success'
                  });
                  // 2.修改页码为第一页
                  this.currentPage = 1;
                  // 3.调用函数刷新列表
                  this.getTypes();
                }else {
                  // 1.给出失败提示
                  this.$message({
                    message: result.msg,
                    type: 'error'
                  });
                  // 2.修改页码为第一页
                  this.currentPage = 1;
                  // 3.调用函数刷新列表
                  this.getTypes();
                }
              })
            }).catch(result=>{
              //  关闭盲灯
              this.editLoading=false;
              //  给出失败提示
              this.$message({
                message: '系统繁忙，请重试！',
                type: 'error'
              });
            })
            this.editFormVisible=false
          }
        });
      },

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
			  //
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.patch("/type",ids).then(result=>{
            result=result.data;
            if(result.success){
              // 删除成功
              // 1.给出成功提示
              this.$message({
                message: result.msg,
                type: 'success'
              });
              // 2.修改页码为第一页
              this.currentPage = 1;
              // 3.调用函数刷新列表
              this.getTypes();
            }else {
              // 删除失败
              // 删除成功
              // 1.给出失败提示
              this.$message({
                message: result.msg,
                type: 'error'
              });
              // 2.修改页码为第一页
              this.currentPage = 1;
              // 3.调用函数刷新列表
              this.getTypes();
            }
          }).catch(result=>{
            // 1.关闭忙等框
            this.listLoading = false;
            // 2.给出自定义失败提示！
            this.$message({
              message: '系统繁忙，请重试！',
              type: 'error'
            });
          })
				}).catch(() => {

				});
			},
      //获取部门列表
      getTypes() {
        let para = {
          currentPage: this.currentPage,
          pageSize:this.pageSize,
          keyword: this.filters.keyword
        };
        //开启盲等
        this.listLoading = true;
        this.$http.post("/type",para).then(res=>{
          //  数据请求成功之后关闭忙等
          this.listLoading=false;
          this.types=res.data.rows;
          this.total=res.data.total
        })
      },

      popDrawer(index,row){
        this.detail=row
        let para={
          currentPage: this.currentPage02,
          pageSize:this.pageSize02,
          keyword: this.filters.keyword,
          typeId:row.id
        }
        this.$http.post("/detail",para).then(result=>{
          this.gridData=result.data.rows
          this.total02=result.data.total
          console.debug("===============")
          console.debug(result.data.rows)
          console.debug("===============")
        })
        this.table=true
      },
      popDrawer02(){

        let para={
          currentPage: this.currentPage02,
          pageSize:this.pageSize02,
          keyword: this.filters.keyword,
          typeId:this.detail.id
        }
        this.$http.post("/detail",para).then(result=>{
          this.gridData=result.data.rows
          this.total02=result.data.total
          console.debug("===============")
          console.debug(result.data.rows)
          console.debug("===============")
        })
        this.table=true
      },

      //删除
      handleDel01: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //NProgress.start();
          let para = { id: row.id };
          this.$http.delete("/type/"+row.id).then(result=>{
            //  关闭盲灯
            this.listLoading = false;
            // 1.获取后端传递的AjaxResult对象
            result = result.data;
            // 2.判断删除是否成功
            if (result.success){
              // 删除成功
              // 1.给出成功提示
              this.$message({
                message: result.msg,
                type: 'success'
              });
              // 2.修改页码为第一页
              this.currentPage = 1;
              // 3.调用函数刷新列表
              this.getTypes();
            }else {
              // 删除失败
              // 删除成功
              // 1.给出失败提示
              this.$message({
                message: result.msg,
                type: 'error'
              });
              // 2.修改页码为第一页
              this.currentPage = 1;
              // 3.调用函数刷新列表
              this.getEmployees();
            }
          })
        }).catch(result=>{
          //  关闭盲灯
          this.listLoading=false;
          //  给出失败提示
          this.$message({
            message: '系统繁忙，请重试！',
            type: 'error'
          });
        }).catch(()=>{

        });
      },
      //抽屉删除
      handleDel02: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //NProgress.start();
          let para = { id: row.id };
          this.$http.delete("/detail/"+row.id).then(result=>{
            //  关闭盲灯
            this.listLoading = false;
            // 1.获取后端传递的AjaxResult对象
            result = result.data;
            // 2.判断删除是否成功
            if (result.success){
              // 删除成功
              // 1.给出成功提示
              this.$message({
                message: result.msg,
                type: 'success'
              });
              // 2.修改页码为第一页
              this.currentPage = 1;
              // 3.调用函数刷新列表
              this.getTypes();
            }else {
              // 删除失败
              // 删除成功
              // 1.给出失败提示
              this.$message({
                message: result.msg,
                type: 'error'
              });
              // 2.修改页码为第一页
              this.currentPage = 1;
              // 3.调用函数刷新列表
              this.getTypes();
            }
          })
        }).catch(result=>{
          //  关闭盲灯
          this.listLoading=false;
          //  给出失败提示
          this.$message({
            message: '系统繁忙，请重试！',
            type: 'error'
          });
        }).catch(()=>{

        });
      },


		},


		mounted() {
			this.getTypes();


		}
	}

</script>

<style scoped>

</style>